#!/bin/sh
#
# Startup script for the Jakarta Jmeter Java load test
#
### BEGIN INIT INFO
# Provides:          jmeter 
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Jmeter.
# Description:       Start the Jakarta Jmeter.
### END INIT INFO
set -e
PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=jmeter
JMETER=jmeter-server
DESC="Jmeter Load Tester"
JMETER_USER=activemq
if [ "$JMETER_USER" = "changeme" ] ; 
then echo "User $JMETER_USER is the default, please create a new one in order to configure jmeter!" && exit 0
elif [ "`grep ^$JMETER_USER: /etc/passwd|cut -d ":" -f1`" != "$JMETER_USER" ];
then echo "User $JMETER_USER not exists, please create it!" && exit 0
fi
JMETER_HOME=/home/$JMETER_USER
DAEMON=$JMETER_HOME/jmeter/bin/jmeter-server
DEFAULT=/etc/default/$NAME
JAVA_PID=`ps ax --width=1000 | grep "[A]pacheJMeter.jar -Dserver_port=1099 -s"|awk '{printf $1 " "}'`
#LOGS="-j $JMETER_HOME/logs/$JMETER.log"

[ -f $JMETER_HOME/jmeter/bin/jmeter-server ] || exit 0
[ -f $JMETER_HOME/jmeter/bin/jmeter ] || exit 0
[ -d $JMETER_HOME/logs ] || su -l $JMETER_USER -c "mkdir logs"

. /lib/lsb/init-functions
. /etc/default/rcS

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/java-6-sun /usr/lib/jvm/java-1.5.0-sun /usr/lib/j2sdk1.5-sun /usr/lib/j2sdk1.5-ibm /usr/lib/j2sdk1.4-sun /usr/lib/j2sdk1.4-blackdown /usr/lib/j2se/1.4 /usr/lib/j2sdk1.4-ibm /usr/lib/j2sdk1.3-sun /usr/lib/j2sdk1.3-blackdown /usr/lib/jvm/java-gcj /usr/lib/kaffe"

# Look for the right JVM to use
for jdir in $JDK_DIRS; do
    if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
        JAVA_HOME_TMP="$jdir"
        # checks for a real JDK like environment, needed to check if 
        # really the java-gcj-compat-dev package is installed
        if [ -r "$jdir/bin/jdb" ]; then
            JAVA_HOME="$JAVA_HOME_TMP"
        fi
    fi
done
export JAVA_HOME

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
        . "$DEFAULT"
fi

test -f $DAEMON || exit 0

#[ -z "$JMETER_USER" ] && JMETER_USER=activemq

# See how we were called.
case "$1" in
  start)
        # Start daemon.
        [ -n "$JAVA_PID" ] && echo "$JMETER already started" && exit 0
	echo -n "Starting $JMETER: "
        su -l $JMETER_USER -c "$JMETER_HOME/jmeter/bin/$JMETER 2>&1 &"
        RETVAL=$?
        MODE="normal"
        echo "$JMETER started in $MODE mode"
        [ $RETVAL = 0 ] && touch /var/lock/subsys/$JMETER
        ;;
  stop)
        # Stop daemons.
	[ -z "$JAVA_PID" ] && echo "$JMETER already stopped" && exit 0
	echo -n "Shutting down $JMETER: "
        kill $JAVA_PID
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/$JMETER
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  condrestart)
       [ -e /var/lock/subsys/$JMETER ] && $0 restart
       ;;
  status)
        if [ -n "$JAVA_PID" ]; then
		MODE="RUNNING pid $JAVA_PID"
	else
		MODE="STOPPED"
	fi
	echo "$JMETER is $MODE"
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit 0
